import NProgress from 'nprogress'
import router from '../routers'
import { useStore } from '../store'
import 'nprogress/nprogress.css'
import { getToken, getOAToken, removeToken } from './auth'

NProgress.configure({ showSpinner: false })

const store = useStore()
const whiteList = ['/login', '/404'] // 路由白名单
const permision = import.meta.env.VUE_APP_PERMISSION_FLAG === 'true' // 是否开启权限管理
const environment = import.meta.env.NODE_ENV === 'production' // 是否为生产环境
const loginBind = import.meta.env.VUE_APP_BIND_LOGIN_IN_PRODUCTION === 'true' // 禁止进入登录页

// 进入路由之前
// eslint-disable-next-line consistent-return
router.beforeEach((to, from, next) => {
  NProgress.start()
  if (!permision) return next()
  // 单点登录校验
  if (getOAToken()) {
    if (getToken()) {
      if (to.path === '/login') return next({ path: '/' })
      if (store.getters.roles.length || to.path.indexOf('/404') !== -1) return next()
      store
        .dispatch('getUserInfo')
        .then(roles => {
          // 拉取用户信息
          // 递归筛根据权限绑定路由
          return store.dispatch('GenerateRoutes', roles)
        })
        .then(() => {
          // 根据roles权限生成可访问的路由表
          store.getters.addRouters.forEach(el => {
            router.addRoute(el)
          }) // 动态添加可访问路由表
          // router.options.routes = router.options.routes.concat(store.getters.addRouters)
          setTimeout(() => {
            next({ ...to, replace: true })
          })
        })
        .catch(err => {
          NProgress.done()
          removeToken()
          next(`/404?msg=${encodeURI(err.msg || err)}`)
        })
    } else {
      if (to.path.indexOf('/404') !== -1) return next()
      store
        .dispatch('oaLogin', getOAToken())
        .then(() => {
          next({ path: '/' })
        })
        .catch(err => {
          removeToken()
          next(`/404?msg=${encodeURI(err.msg || err)}`)
        })
    }
    // 登录校验
  } else if (getToken()) {
    if (to.path === '/login') return next({ path: '/' })
    if (store.getters.roles.length || to.path.indexOf('/404') !== -1) return next()
    store
      .dispatch('getUserInfo')
      .then(roles => {
        // 拉取用户信息
        // 递归筛根据权限绑定路由
        return store.dispatch('GenerateRoutes', roles)
      })
      .then(() => {
        // 根据roles权限生成可访问的路由表
        store.getters.addRouters.forEach(el => {
          router.addRoute(el)
        }) // 动态添加可访问路由表
        // router.options.routes = router.options.routes.concat(store.getters.addRouters)
        setTimeout(() => {
          next({ ...to, replace: true })
        })
      })
      .catch(err => {
        NProgress.done()
        removeToken()
        next(`/404?msg=${encodeURI(err.msg || err)}`)
      })
  } else {
    NProgress.done()
    const isInwhiteList = whiteList.indexOf(to.path) !== -1
    if ((isInwhiteList && to.path !== '/login') || (isInwhiteList && (loginBind ? !environment : true))) return next()
    if (!(environment && loginBind)) return next('/login')
    next(`/404?msg=${encodeURI('请通过OA单点登录')}`)
  }
})

// 进入路由之后
router.afterEach(() => {
  NProgress.done()
})
